package com.huanxing.cloud.mall.api.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanxing.cloud.common.core.entity.Result;
import com.huanxing.cloud.mall.api.service.IGoodsSpuService;
import com.huanxing.cloud.mall.api.utils.HxTokenHolder;
import com.huanxing.cloud.mall.common.entity.GoodsSpu;
import com.huanxing.cloud.mall.common.entity.UserFootprint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品spu
 *
 * @author lijx
 * @since 2022/3/1 10:13
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/goodsspu")
@Api(value = "goodsspu", tags = "商品spu")
public class GoodsSpuController {
  private final IGoodsSpuService goodsSpuService;

  @ApiOperation(value = "商品列表")
  @GetMapping("/page")
  public Result page(Page page, GoodsSpu goodsSpu) {
    return Result.success(goodsSpuService.apiPage(page, goodsSpu));
  }

  @ApiOperation(value = "通过id查询商品")
  @GetMapping("/{id}")
  public Result getById(@PathVariable String id) {
    GoodsSpu goodsSpu = goodsSpuService.getSpuById(id);

    // 保存用户足迹
    if (StrUtil.isNotBlank(HxTokenHolder.getMallUserId())) {
      try {
        UserFootprint userFootprint = new UserFootprint();
        userFootprint.setUserId(HxTokenHolder.getMallUserId());
        userFootprint.setSpuId(goodsSpu.getId());
        userFootprint.insert();
      } catch (Exception e) {
        log.error("保存用户足迹异常");
      }
    }
    return Result.success(goodsSpu);
  }
}
